﻿using CATL.PrintMarkingServer.DAL.DBConnectionHelper;
using CenterControllerServer.WebApi.Modles;
using Microsoft.AspNetCore.Mvc;
using StackExchange.Redis;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace CenterControllerServer.WebApi.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class SqlClientController : ControllerBase
    {
        private readonly IDatabase _db;
        private readonly IConnectionMultiplexer _redis;
        private readonly SqlServerClient _client;

        public SqlClientController(IServiceProvider service, IConnectionMultiplexer redis, SqlServerClient client)
        {
            _db = redis.GetDatabase();
            _redis = redis;
            _client = client;
        }

        [HttpGet]
        public async Task<ResponseMessage<DataTable>> Get(string key)
        {
            var valus = await _db.ListRangeAsync(key);
            var list = valus.Select(item =>
            {
                string strValue = item;
                return strValue;
            });

            return new ResponseMessage<DataTable>()
            {
                Content = new DataTable()
                {
                    Key = key,
                    Values = list
                }
            };
        }

        [HttpPost]
        public async Task<ResponseMessage<string>> Post(string key, string value)
        {
            await _db.ListRightPushAsync(key, value);
            return new ResponseMessage<string>();
        }

#if DEBUG
        [HttpPut]
        public async void AddDataSeed()
        {
            var task1 = Task.Run(() =>
            {
                for (int i = 0; i < 150000; i++)
                {
                    var values = new RedisValue[] { Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString() };
                    _db.ListRightPush($"Key{i + 1}", values);
                    Console.WriteLine(i);
                }
            });

            await task1;
        }
#endif

    }
}
